//
//  ViewController.m
//  BabyAlbum
//
//  Created by master on 17/2/13.
//  Copyright © 2017年 master. All rights reserved.
//

#import "BAAlbumScrollVC.h"
#import "BAAlbumCell.h"
#import "BAAlbumScrollLayout.h"
#import "BAAlbumScrollCellNode.h"
#import "BAAlbumDetailVC.h"
#import "BAResource.h"
@interface BAAlbumScrollVC () <UICollectionViewDelegate,ASCollectionDelegate,ASCollectionDataSource, ASCollectionDelegateFlowLayout,BAAlbumDataProtocol,UIViewControllerTransitioningDelegate>
@property (nonatomic, strong) NSArray *datas;

@end


static NSString *cellIndentifier = @"BAAlbumCell";
@implementation BAAlbumScrollVC

- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize {
    return [ASInsetLayoutSpec insetLayoutSpecWithInsets:UIEdgeInsetsMake(0, 0, 0, 0) child:self.collectionNode];
}

- (NSArray *)datas {
    return @[];
}

//- (instancetype)init {
//    if(self = [super initWithNode:[[ASDisplayNode alloc] init]]) {
//        
//    }
//    return self;
//}

- (void)didLoad {
    [super didLoad];
    
    [self viewInit];

    [BAALBUM_DATA setDelegate:self];

    
}

//- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
//    NSLog(@"%@",change);
//}

//- (void)viewDidLayoutSubviews {
//    [super viewDidLayoutSubviews];
//    self.node.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
//
//}

- (void)viewInit {
    
    self.backgroundColor = RGB_255(235, 240, 239);
    BAAlbumScrollLayout *layout = [[BAAlbumScrollLayout alloc] init];
    layout.itemSize = CGSizeMake(kScreenWidth - 80, kScreenHeight - 160);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    
    self.collectionNode = [[ASCollectionNode alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) collectionViewLayout:layout];
    
    self.collectionNode.dataSource = self;
    self.collectionNode.delegate = self;
    self.collectionNode.view.showsHorizontalScrollIndicator = NO;

//    self.collectionNode.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    self.collectionNode.backgroundColor = RGB_255(235, 240, 239);
    
    self.collectionNode.view.contentInset = UIEdgeInsetsMake(0, 40, 0, 40);
//    self.automaticallyAdjustsScrollViewInsets = YES;
//    self.automaticContentOffsetAdjustmentEnabled
//    [self.collectionNode.view addObserver:self forKeyPath:@"bounds" options:NSKeyValueObservingOptionNew context:nil];
    //    [self.collectionNode.view registerNib:[UINib nibWithNibName:@"BAAlbumCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:cellIndentifier];
    [self addSubnode:self.collectionNode];

    
    UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
    addButton.frame = CGRectMake(0, 0, 55, 55);
    addButton.center = CGPointMake(kScreenWidth / 2 , self.view.bottom - 55);
    [addButton setImage:[UIImage imageNamed:@"add.png"] forState:UIControlStateNormal];
    [addButton addTarget:self action:@selector(addAlbum) forControlEvents:UIControlEventTouchUpInside];
    addButton.layer.shadowOffset = CGSizeMake(0, 3);
    addButton.layer.shadowColor = RGBA_255(186, 221, 218,1).CGColor;
    addButton.layer.shadowRadius = 4;
    addButton.layer.shadowOpacity = 0.6;
    [self.view addSubview:addButton];



}

//- (void)viewWillAppear:(BOOL)animated {
//    [super viewWillAppear:animated];
////    [self.collectionNode reloadData];
//
//}

- (void)addAlbum {
    BAAlbumDetailVC *detail = [[BAAlbumDetailVC alloc] initWithNewAlbum];
    [_manager.navigationController pushViewController:detail animated:YES];

}




#pragma mark - ASCollectionView Data Source

- (ASCellNodeBlock)collectionNode:(ASCollectionNode *)collectionNode nodeBlockForItemAtIndexPath:(NSIndexPath *)indexPath;
{
    BAAlbumModel *model = BAALBUM_DATA.albumList[indexPath.row];
    return ^{
//        UIImage *image = [model.cover coverImage];

        return [[BAAlbumScrollCellNode alloc] initWithModel:model];
    };

}


- (NSInteger)collectionNode:(ASCollectionNode *)collectionNode numberOfItemsInSection:(NSInteger)section
{
    return BAALBUM_DATA.albumList.count;
}

- (NSInteger)numberOfSectionsInCollectionNode:(ASCollectionNode *)collectionNode
{
    return 1;
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSArray<ASCellNode *> *array = [self.collectionNode visibleNodes];
    if(!array.count) {
        return;
    }
    [BAALBUM_DATA removeDelegate:self];
    BAALBUM_DATA.currentIndex = [array lastObject].indexPath.row;
    [BAALBUM_DATA setDelegate:self];
}

//- (void)viewDidAppear:(BOOL)animated {
//    [super viewDidAppear:animated];
//}
- (void)collectionNode:(ASCollectionNode *)collectionNode didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [BAALBUM_DATA removeDelegate:self];
    BAALBUM_DATA.currentIndex = indexPath.row;
    [BAALBUM_DATA setDelegate:self];

    BAAlbumDetailVC *detail = [[BAAlbumDetailVC alloc] init];
//    BAAlbumScrollCellNode *node = [collectionNode nodeForItemAtIndexPath:indexPath];
//    UIViewController *vc =[[UIViewController alloc] init];
    [_manager.navigationController pushViewController:detail animated:YES];
}

- (void)albumManager:(BAAlbumIndexManager *)manager didChangeToIndex:(NSInteger)index {
    if([self.collectionNode numberOfItemsInSection:0] > BAALBUM_DATA.currentIndex && self.collectionNode.view.numberOfSections) {
        [self.collectionNode scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
        NSLog(@"Scroll Cover 变更Index:%ld",index);

    }
}

- (void)albumManagerDataSourceDidChanged:(BAAlbumIndexManager *)manager {
    [self.collectionNode reloadData];
    NSLog(@"Scroll Cover 已刷新");
}


@end
